package com.jurenzhao.partssystem.information.basic_series.controller;

import com.jurenzhao.partssystem.information.basic_series.entity.BasicSeries;
import com.jurenzhao.partssystem.information.basic_series.service.BasicSeriesService;
import com.jurenzhao.partssystem.information.basic_series.util.PathImg;
import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/series")
public class BasicSeriesController {
    @Resource
    private BasicSeriesService basicSeriesService;
    @RequestMapping("/list")
    public String BasicSeriesList(Model model){
        List<BasicSeries> basicSeries =  basicSeriesService.findAll();
        model.addAttribute("basicSeries",basicSeries);
        return "/information/BasicSeries/BasicSeries_List";
    }

    @RequestMapping("/preAdd")
    public String BasicSeriesPreAdd(){
        return "/information/BasicSeries/BasicSeries_Add";
    }
    @RequestMapping("add")
    public String BasicSeriesAdd(BasicSeries basicSeries, MultipartFile file) throws IOException {
        // 唯一标识
        String imagePath = UUID.randomUUID().toString();
        System.out.println(imagePath);
        // 创建一个新文件以保存上传的文件
        File newFile = new File(PathImg.IMAGE_BASE_PATH + imagePath);
        // 将上传的文件保存到新文件中
        file.transferTo(newFile);
        // 将文件名存储到数据库中
        basicSeries.setSeriesPic(imagePath);
        System.out.println(basicSeries);
        int count = basicSeriesService.save(basicSeries);
        return "redirect:/series/list";
    }
}
